package com.example.androidstart;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.androidstart.util.FileUtil;

public class TestFileReadSpeed extends AppCompatActivity {
    private static final String TAG = "TestFileReadSpeed";

    String path = "/data/data/com.example.androidstart/cache/testFile.txt";
    String smallFilePath = "/data/data/com.example.androidstart/cache/test_read_file.txt";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_file_read_speed);
        findViewById(R.id.readFileByBufferReaderStringBuilder).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                long timeStart = System.currentTimeMillis();
                String result = FileUtil.ReadFileByBufferReaderToString(path);
                Log.i(TAG, "onClick: contentlenght readFileByBufferReaderStringBuilder " + result.length());
                Log.i(TAG, "onClick: readFileByBufferReaderStringBuilder tims use is " + (System.currentTimeMillis() - timeStart));
            }
        });
        findViewById(R.id.readFileByBufferReaderString).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "onClick: start read");
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Log.i(TAG, "onClick: start begin read");
                        long timeStart = System.currentTimeMillis();
                        String result = FileUtil.ReadFileByBufferReaderToStringUseString(path);
                        Log.i(TAG, "onClick: contentlenght readFileByBufferReaderString " + result.length());
                        Log.i(TAG, "onClick: readFileByBufferReaderString tims use is " + (System.currentTimeMillis() - timeStart));
                    }
                });
                thread.start();
            }
        });
        findViewById(R.id.ReadFileByreadAllBytesReaderToString).setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onClick(View v) {
                Thread thread=new Thread(new Runnable() {
                    @Override
                    public void run() {
                        long timeStart = System.currentTimeMillis();
                        String result = FileUtil.ReadFileByReadAllBytesReaderToString(path);
                        Log.i(TAG, "onClick: contentlenght ReadFileByReadAllBytesReaderToString " + result.length());
                        Log.i(TAG, "onClick: ReadFileByReadAllBytesReaderToString tims use is " + (System.currentTimeMillis() - timeStart));
                    }
                });
                thread.start();
            }
        });
        findViewById(R.id.ReadFileByByFilesReadLinesToString).setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onClick(View v) {
                long timeStart = System.currentTimeMillis();
                String result = FileUtil.ReadFileByByFilesReadLinesToString(path);
                Log.i(TAG, "onClick: contentlenght ReadFileByByFilesReadLinesToString " + result.length());
                Log.i(TAG, "onClick: ReadFileByByFilesReadLinesToString tims use is " + (System.currentTimeMillis() - timeStart));
            }
        });
        findViewById(R.id.ReadFileByCommonIOReadFileToString).setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onClick(View v) {
                long timeStart = System.currentTimeMillis();
                String result = FileUtil.ReadFileByCommonIOReadFileToString(path);
                Log.i(TAG, "onClick: contentlenght ReadFileByCommonIOReadFileToString " + result.length());
                Log.i(TAG, "onClick: ReadFileByCommonIOReadFileToString tims use is " + (System.currentTimeMillis() - timeStart));
            }
        });
    }
}